home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-01 / strlib.zip / STRREV.C < prev    next >
Text File  |  1993-01-04  |  1KB  |  42 lines

  1.  
  2. /*  File   : strrev.c
  3.     Author : Richard A. O'Keefe.
  4.     Updated: 1 June 1984
  5.     Defines: strrev()
  6.  
  7.     strrev(dst, src)
  8.     copies all the characters of src to dst, in REVERSE order.   Dst is
  9.     properly terminated with a NUL character.  There is no result.
  10.     Example: strrev(x, "able was I er") moves "re I saw elba" to x.
  11.  
  12.     Note: this function is perfectly happy to reverse a string into the
  13.     same place, strrev(x, x) will work.  That is why it looks so hairy.
  14.     It will not work for partially overlapping source and destination.
  15. */
  16.  
  17. #include "strings.h"
  18.  
  19. void strrev(dsta, srca)
  20.     register char *dsta, *srca;
  21.     {
  22.         register char *dstz, *srcz;
  23.         register int t;                 /* should be char */
  24.  
  25.         for (srcz = srca; *srcz++; ) ;
  26.         srcz--;
  27.         dstz = dsta+(srcz-srca);
  28.         /*  Now srcz points to the NUL terminating src,
  29.             and dstz points to where the terminating NUL for dst belongs.
  30.         */
  31.         *dstz = NUL;
  32.         while (srcz > srca) {
  33.             /*  This is guaranteed safe by K&R, since srcz and srca
  34.                 point "into the same array".
  35.             */
  36.             t = *--srcz;
  37.             *--dstz = *srca++;
  38.             *dsta++ = t;
  39.         }
  40.     }
  41.  
  42.